package com.taobao.txc.common.util.serviceloader;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

public class a
{
  private static final LoggerWrap a = LoggerInit.logger;
  private static Map<Class, List<Class>> b = new ConcurrentHashMap();
  
  public static <S> S a(Class<S> paramClass)
  {
    return a(paramClass, null, a());
  }
  
  private static <S> S a(Class<S> paramClass, String paramString, ClassLoader paramClassLoader)
  {
    try
    {
      int i = 1;
      Object localObject1 = (List)b.get(paramClass);
      if (localObject1 == null) {
        synchronized (paramClass)
        {
          localObject1 = (List)b.get(paramClass);
          if (localObject1 == null)
          {
            localObject1 = b(paramClass, paramString, paramClassLoader);
            i = 0;
            b.put(paramClass, localObject1);
          }
        }
      }
      if (StringUtils.isNotEmpty(paramString))
      {
        a(paramClass, "META-INF/txc/" + paramString.toLowerCase() + "/", paramClassLoader, (List)localObject1);
        ??? = new ArrayList();
        for (int j = 0; j < ((List)localObject1).size(); j++)
        {
          Class localClass = (Class)((List)localObject1).get(j);
          LoadLevel localLoadLevel = (LoadLevel)localClass.getAnnotation(LoadLevel.class);
          if ((localLoadLevel != null) && (paramString.equals(localLoadLevel.name()))) {
            ((List)???).add(localClass);
          }
        }
        localObject1 = ???;
      }
      if (((List)localObject1).isEmpty()) {
        throw new c("not found service provider for : " + paramClass.getName() + "[" + paramString + "] and classloader : " + ObjectUtils.toString(paramClassLoader));
      }
      ??? = (Class)((List)localObject1).get(((List)localObject1).size() - 1);
      Object localObject3 = paramClass.cast(((Class)???).newInstance());
      if ((i == 0) && (a.d())) {
        a.info("load " + paramClass.getSimpleName() + "[" + paramString + "] extension by class[" + ((Class)???).getName() + "]");
      }
      return localObject3;
    }
    catch (Throwable localThrowable)
    {
      if ((localThrowable instanceof c)) {
        throw ((c)localThrowable);
      }
      throw new c("not found service provider for : " + paramClass.getName() + " caused by " + ExceptionUtils.getFullStackTrace(localThrowable));
    }
  }
  
  private static <S> List<Class> b(Class<S> paramClass, String paramString, ClassLoader paramClassLoader)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      a(paramClass, "META-INF/services/", paramClassLoader, localArrayList);
      a(paramClass, "META-INF/txc/", paramClassLoader, localArrayList);
    }
    catch (IOException localIOException)
    {
      throw new c(localIOException);
    }
    if (localArrayList.isEmpty()) {
      return localArrayList;
    }
    Collections.sort(localArrayList, new b());
    return localArrayList;
  }
  
  private static void a(Class<?> paramClass, String paramString, ClassLoader paramClassLoader, List<Class> paramList)
  {
    String str1 = paramString + paramClass.getName();
    Enumeration localEnumeration;
    if (paramClassLoader != null) {
      localEnumeration = paramClassLoader.getResources(str1);
    } else {
      localEnumeration = ClassLoader.getSystemResources(str1);
    }
    for (;;)
    {
      if ((localEnumeration != null) && (localEnumeration.hasMoreElements()))
      {
        URL localURL = (URL)localEnumeration.nextElement();
        BufferedReader localBufferedReader = null;
        try
        {
          localBufferedReader = new BufferedReader(new InputStreamReader(localURL.openStream(), "utf-8"));
          String str2 = null;
          while ((str2 = localBufferedReader.readLine()) != null)
          {
            int i = str2.indexOf('#');
            if (i >= 0) {
              str2 = str2.substring(0, i);
            }
            str2 = str2.trim();
            if (str2.length() > 0) {
              paramList.add(Class.forName(str2, true, paramClassLoader));
            }
          }
          try
          {
            if (localBufferedReader != null) {
              localBufferedReader.close();
            }
          }
          catch (IOException localIOException1) {}
        }
        catch (ClassNotFoundException localClassNotFoundException) {}catch (Throwable localThrowable)
        {
          a.warn(localThrowable.getMessage());
        }
        finally
        {
          try
          {
            if (localBufferedReader != null) {
              localBufferedReader.close();
            }
          }
          catch (IOException localIOException4) {}
        }
      }
    }
  }
  
  private static ClassLoader a()
  {
    return a.class.getClassLoader();
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.common.util.serviceloader.a
 * JD-Core Version:    0.7.0.1
 */